package com.base.controller.utils;

import com.base.controller.utils.Md5.Md5EncryptUtils;
import com.base.helper.VosHelper;
import com.base.po.param.VosModelList;
import com.base.po.voip.Callbackinfoes;
import com.base.utils.GsonUtil;
import com.base.utils.MapUtils;
import lombok.extern.log4j.Log4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 随便打 的工具类
 */
@Log4j
@Component
public class SBDUtil {
    public static SBDUtil sbdUtil;

    private static String sbdsecret="sbdcallback1.0";


    @PostConstruct
    public void init(){
        sbdUtil=this;
    }

//    @Autowired
//

    /**
     *  @Date: 2020/6/10 15:31
     *  @Description:
     *  获取用户的当前话费余额
     */
    public static BigDecimal getUserMoney(String agentprefix,String mobile){
        String result2 = VosHelper.GetVosAccount("{accounts:['" + agentprefix + mobile + "']}");
        VosModelList v2 = GsonUtil.getObject(result2, VosModelList.class);
        BigDecimal balance2 = v2.infoCustomers[0].money; // 用户余额
        return balance2;
    }

    /**
     * sf.json转map
     * @param jsonObject
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> JsontoMap(JSONObject jsonObject) {
        Map<String, String> result = new HashMap<String, String>();
        Iterator<String> iterator = jsonObject.keys();
        String key = null;
        String value = null;
        while (iterator.hasNext()) {
            key = iterator.next();
            value = jsonObject.getString(key);
            result.put(key, value);
        }
        return result;
    }

    /**
     * 获取回调的完整参数
     * @param callbackinfoes
     * @return
     */
    public static com.alibaba.fastjson.JSONObject callbackbyurl(Callbackinfoes callbackinfoes) {

        Map<String,Object> map= MapUtils.object2Map(callbackinfoes);
        map.put("calltime",callbackinfoes.getCalltime()==null?null:callbackinfoes.getCalltime().getTime());
        map.put("fwdtime",callbackinfoes.getFwdtime()==null?null:callbackinfoes.getFwdtime().getTime());
        map.put("endtime",callbackinfoes.getEndtime()==null?null:callbackinfoes.getEndtime().getTime());
        map.put("money",callbackinfoes.getMoney()==null?null:callbackinfoes.getMoney().setScale(2, RoundingMode.HALF_DOWN));
        map.put("omoney",callbackinfoes.getOmoney()==null?null:callbackinfoes.getOmoney().setScale(2, RoundingMode.HALF_DOWN));

//        Map<String,String> map1=Maps.newHashMap();
//        map.forEach((k, v) -> {
//            if (v!=null && !v.equals("")){
//                map1.put(k,v.toString());
//            }
//       });


        String sign=getSign(map,"sbdcallback1.0");
        map.put("sign",sign);
        com.alibaba.fastjson.JSONObject jsonObject= new com.alibaba.fastjson.JSONObject(map);
        return jsonObject;
    }

    public static String getSign(Map<String, Object> paramsMap, String appSecret) {
        // 返回MD5签名
        String sign = "";

        try {
            List<String> params = new ArrayList<>();
            for (String key : paramsMap.keySet()) {
                if (paramsMap.get(key)!=null && paramsMap.get(key)!=""){
                    String values = key + paramsMap.get(key);
                    params.add(values);
                }
            }
            log.warn("加密前参数:"+params.toString());
            if (params != null && params.size() > 0) {
                //String.CASE_INSENSITIVE_ORDER A在 a 前面
                Collections.sort(params, String.CASE_INSENSITIVE_ORDER);
                // 获取拼接参数
                String param = "";
                for (int i = 0; i < params.size(); i++) {
                    param += params.get(i);
                }
                // 参数不为空 拼接key
                if (param != null && !param.equals("")) {
                    param = appSecret + param + appSecret;
                }
                log.warn("拼接后参数:"+param);
                sign = Md5EncryptUtils.encrypt32(param);
            }
        } catch (Exception e) {
            System.out.println("API签名生成失败");
        }
        log.warn("sign值"+sign);
        return sign;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
//       String jutil="{\"ipaddress\":\"127.0.0.1\",\"country\":null,\"fwdtime\":1597313040000,\"sysnum\":\"JH推送话单\",\"city\":null,\"timecount\":9,\"endtime\":1597313029000,\"parentid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calltime\":1597313029000,\"omoney\":0.09,\"caller\":\"18513662019\",\"calle164\":\"18055603791\",\"money\":0.2,\"province\":null,\"calltimeStr\":null,\"endTimeStr\":null,\"shopid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calluptime\":null,\"id\":\"oem:11bd1413f3d747e4bd68485676b654e9\",\"wxopenid\":null,\"fwdTimeStr\":null,\"status\":\"接通\"}";
//        String jutil="{\"ipaddress\":\"192.168.255.46\",\"country\":\"中国\",\"fwdtime\":1597742844000,\"sysnum\":\"JH推送话单\",\"city\":\"杭州市\",\"timecount\":159,\"endtime\":1597743678000,\"parentid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calltime\":1597743678000,\"omoney\":0.27,\"caller\":\"13607267085\",\"calle164\":\"13023688579\",\"money\":0.6,\"province\":\"浙江省\",\"calltimeStr\":null,\"endTimeStr\":null,\"shopid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calluptime\":null,\"id\":\"71a236df80c5467bb16f90326fcbdd7a\",\"wxopenid\":null,\"fwdTimeStr\":null,\"status\":\"接通\"}";
//        String jutil="{\"ipaddress\":\"192.168.255.46\",\"country\":\"中国\",\"fwdtime\":1597742844000,\"sysnum\":\"JH推送话单\",\"city\":\"杭州市\",\"timecount\":159,\"endtime\":1597743678000,\"parentid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calltime\":1597743678000,\"omoney\":0.27,\"caller\":\"13607267085\",\"calle164\":\"13023688579\",\"money\":0.6,\"province\":\"浙江省\",\"calltimeStr\":null,\"endTimeStr\":null,\"shopid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calluptime\":null,\"id\":\"71a236df80c5467bb16f90326fcbdd7a\",\"wxopenid\":null,\"fwdTimeStr\":null,\"status\":\"接通\"}";
//       String jutil="{\"ipaddress\":\"192.168.255.46\",\"country\":\"中国\",\"fwdtime\":1597747280000,\"sysnum\":\"JH推送话单\",\"city\":\"杭州市\",\"timecount\":6,\"endtime\":1597748127000,\"parentid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calltime\":1597748127000,\"omoney\":0.09,\"caller\":\"13607267085\",\"calle164\":\"13023688579\",\"money\":0.2,\"province\":\"浙江省\",\"calltimeStr\":null,\"endTimeStr\":null,\"shopid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calluptime\":null,\"id\":\"a94d5b5a146045708e8536906f8ec149\",\"wxopenid\":null,\"fwdTimeStr\":null,\"status\":\"接通\"}";
//        String jutil="{\"ipaddress\":\"192.168.255.46\",\"country\":\"中国\",\"fwdtime\":1597748475000,\"sysnum\":\"JH推送话单\",\"city\":\"杭州市\",\"timecount\":6,\"endtime\":1597749313000,\"parentid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calltime\":1597749313000,\"omoney\":0.09,\"caller\":\"13607267085\",\"calle164\":\"13023688579\",\"money\":0.2,\"province\":\"浙江省\",\"calltimeStr\":null,\"endTimeStr\":null,\"shopid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calluptime\":null,\"id\":\"51eb250d9441485bad978dee334d654d\",\"wxopenid\":null,\"fwdTimeStr\":null,\"status\":\"接通\"}";        JSONObject jsonObject=JSONObject.fromObject(jutil);
        String jutil="{\"ipaddress\":\"192.168.255.46\",\"country\":\"中国\",\"fwdtime\":1597818037000,\"sysnum\":\"JH推送话单\",\"city\":\"杭州市\",\"timecount\":26,\"endtime\":1597818878000,\"parentid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"calltime\":1597818878000,\"omoney\":0.09,\"caller\":\"13607267085\",\"calle164\":\"13023688579\",\"money\":0.20,\"province\":\"浙江省\",\"shopid\":\"2b736eabf5af4d7f95ebfd0d92c35d63\",\"id\":\"e1613eef4f1c4063997b7fec28b86886\",\"status\":\"接通\"}";
        com.alibaba.fastjson.JSONObject jsonObject1= com.alibaba.fastjson.JSONObject.parseObject(jutil);
        Map<String,Object> map=jsonObject1.getInnerMap();
       String sign= getSign(map,sbdsecret);
        map.put("sign",sign);
        log.warn("[回调接受]"+map.toString());

    }

}
